package com.tools.web.interceptor.auth.custom;

import com.tools.common.object.Note;
import com.tools.common.security.secret.TwoWayCypher;
import com.tools.web.interceptor.auth.AuthSource;
import com.tools.web.interceptor.auth.AuthToken;
import com.tools.web.interceptor.auth.AuthUserService;
import com.tools.web.interceptor.auth._common.LocalMultipleAuthenticationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 使用【JSON + 自定义本地缓存】 实现的 “用户多在线记录” 的 AuthenticationManager 身份认证拦截器
 * */
@Note("使用【JSON + 自定义本地缓存】 实现的 “用户多在线记录” 的 AuthenticationManager 身份认证拦截器")
public final class JSONLocalMultipleAuthenticationManager extends LocalMultipleAuthenticationManager {

    @Note("用于双向加密解密 AuthToken 的工具实例")
    private final TwoWayCypher cypher;

    @Note("日志输出对象")
    private static final Logger LOGGER = LoggerFactory.getLogger(JSONLocalMultipleAuthenticationManager.class);

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public JSONLocalMultipleAuthenticationManager(AuthSource source, TwoWayCypher cypher) {
        this(1, TimeUnit.HOURS, source, null, cypher);
    }

    public JSONLocalMultipleAuthenticationManager(AuthSource source, AuthUserService authUserService,
                                                  TwoWayCypher cypher) {
        this(1, TimeUnit.HOURS, source, authUserService, cypher);
    }

    public JSONLocalMultipleAuthenticationManager(long execTime, TimeUnit execTimeUnit,
                                                 AuthSource source, TwoWayCypher cypher) {
        this(execTime, execTimeUnit, source, null, cypher);
    }

    public JSONLocalMultipleAuthenticationManager(long execTime, TimeUnit execTimeUnit, AuthSource source,
                                                 AuthUserService authUserService, TwoWayCypher cypher) {
        super(execTime, execTimeUnit, source, authUserService);
        this.cypher = Objects.requireNonNull(cypher, "自定义实现的 AuthToken 用于双向加密解密的工具 TwoWayCypher 实例不能为 null");
    }


    /* **************************************************************************************
     *
     *          实现来自接口或父类的抽象方法
     *
     * **************************************************************************************
     * */


    @Override
    protected Logger protected_getLogger() {
        return LOGGER;
    }

    @Override
    protected String protected_getInstanceName() {
        return Integer.toHexString(this.hashCode());
    }

    @Override
    public AuthToken decryptAuthToken(String encryptedToken) {
        return new JSONAuthToken(this.cypher, encryptedToken);
    }

    @Override
    public AuthToken newEmptyAuthToken() {
        return new JSONAuthToken(this.cypher);
    }

    public TwoWayCypher getCypher() {
        return cypher;
    }


}
